import 'dart:async';
import 'package:flutter/material.dart';
import '../constants.dart';

class Carousel extends StatefulWidget {
  @override
  _CarouselState createState() => _CarouselState();
}

class _CarouselState extends State<Carousel> {
  Timer _timer;
  PageController _pageController;
  int _currentPage = 10000 % urlsCarouselImages.length;

  @override
  void initState() {
    _pageController = PageController(
      initialPage: 10000,
    );
    _timer = Timer.periodic(Duration(milliseconds: 3000), (timer) {
      _pageController.nextPage(duration: Duration(milliseconds: 300), curve: Curves.linear);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.bottomRight,
      children: [
        Container(
          height: 100,
          child: NotificationListener<ScrollNotification>(
            onNotification: (notification) {
              if (notification is UserScrollNotification) {}
              return true;
            },
            child: PageView.builder(
              controller: _pageController,
              itemBuilder: (BuildContext context, int index) {
                int realIndex = index % urlsCarouselImages.length;
                return Container(
                  margin: EdgeInsets.symmetric(horizontal: margin10),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(16),
                    child: Image.network(
                      urlsCarouselImages[realIndex],
                      fit: BoxFit.cover,
                    ),
                  ),
                );
              },
              itemCount: null,
              onPageChanged: (i) {
                setState(() {
                  _currentPage = i % urlsCarouselImages.length;
                });
              },
            ),
          ),
        ),
        Container(
            padding: EdgeInsets.all(4),
            margin: EdgeInsets.only(right: margin10),
            decoration: BoxDecoration(
              color: Color.fromARGB(160, 255, 255, 255),
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10),
                  bottomRight: Radius.circular(10)),
            ),
            child: Text(
              "资质与规则",
              style: TextStyle(fontSize: 12, color: Colors.black54),
            )),
        Transform.translate(
          offset: Offset(0, -8),
          child: SizedOverflowBox(
            size: Size(double.infinity, 0),
            child: Container(
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  ...List.generate(urlsCarouselImages.length, (index) {
                    if (_currentPage != index)
                      return Padding(
                        padding: const EdgeInsets.all(2),
                        child: Icon(
                          Icons.circle,
                          color: Colors.white54,
                          size: 4,
                        ),
                      );
                    else
                      return Padding(
                        padding: const EdgeInsets.all(2),
                        child: Image.asset(
                          "assets/wm_common_arch_dot_white.webp",
                          width: 8,
                          height: 5,
                        ),
                      );
                  })
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }
  @override
  void dispose() {
    _timer.cancel();
    _pageController.dispose();
    super.dispose();
  }
}